package com.manager.action.admin;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.common.util.TranslateHelper;
import org.wface.web.core.ActionSupport;

import com.manager.model.SysAdmin;
import com.manager.model.SysRole;
import com.manager.service.system.AdminService;
import com.web.RandomCode;
import com.web.tag.bean.PageModel;

public class AdminAction extends ActionSupport {

	private String username;
	private String password;
	private String randcode;
	private String name;
	private String oldPwd;
	private String newPwd;
	private String newPwdz;
	private String page;
	private String state;
	private String id;
	private String updatePwd;
	private String[] check;

	public String login() {
		AdminService adminService = new AdminService();
		SysAdmin admin = adminService.login(username, password);
		if (admin != null) {
			getRequest().getSession().setAttribute("sysAdmin", admin);
			return "manager_main";
		} else {
			getRequest().setAttribute("loginMsg", "输入用户名或密码不正确!");
			return "login";
		}
	}

	public String refresh() {
		HttpSession session = getRequest().getSession();
		AdminService adminService = new AdminService();
		SysAdmin admin = (SysAdmin) session.getAttribute("sysAdmin");
		if (admin == null) {
			getRequest().setAttribute("loginMsg", "管理员登录超时!");
			return "login";
		} else {
			admin = adminService.login(admin.getLoginName(), admin.getLoginPass(), false);
		}
		if (admin != null) {
			session.setAttribute("sysAdmin", admin);
			return "manager_main";
		} else {
			getRequest().setAttribute("loginMsg", "管理员登录信息有误!");
			return "login";
		}
	}

	public String exit() {
		HttpSession session = getRequest().getSession();
		session.removeAttribute("sysAdmin");
		return "manager_login";
	}

	public String selfInfo() {
		HttpSession session = getRequest().getSession();
		SysAdmin admin = (SysAdmin) session.getAttribute("sysAdmin");
		if (admin != null) {
			admin.setName(name);
			AdminService adminService = new AdminService();
			if (adminService.updateAdminInfo(admin)) {
				getRequest().setAttribute("infoMsg", "基本信息更新成功!");
			} else {
				getRequest().setAttribute("infoMsg", "基本信息更新失败!");
			}
			return "self";
		} else {
			return "self_redirect";
		}
	}

	public String selfPwd() {
		HttpSession session = getRequest().getSession();
		SysAdmin admin = (SysAdmin) session.getAttribute("sysAdmin");
		if (admin != null) {
			admin.setLoginPass(newPwd);
			AdminService adminService = new AdminService();
			if (adminService.updateAdminPwd(admin, oldPwd)) {
				getRequest().setAttribute("pwdMsg", "密码更新成功!");
			} else {
				getRequest().setAttribute("pwdMsg", "密码更新失败!");
			}
			return "self";
		} else {
			return "self_redirect";
		}
	}

	public String list() {
		adminListPage(null);
		return "adminList";
	}

	public String edit() {
		AdminService adminService = new AdminService();
		SysAdmin admin = adminService.findAdminById(TranslateHelper.parseInteger(id));
		getRequest().setAttribute("editAdmin", admin);
		// ******************************
		adminListPage(adminService);
		return "adminList";
	}

	public String save() {
		SysAdmin admin = new SysAdmin();
		admin.setId(TranslateHelper.parseInteger(id));
		admin.setLoginName(username);
		admin.setLoginPass(password);
		admin.setName(name);
		admin.setRegIp(getRequest().getRemoteAddr());
		admin.setRegTime(new Timestamp(System.currentTimeMillis()));
		admin.setState(TranslateHelper.parseInteger(state));
		AdminService adminService = new AdminService();
		if (adminService.saveAdmin(admin, "1".equals(updatePwd) ? true : false)) {
			getRequest().setAttribute("saveMsg", "管理员保存或修改成功!");
		} else {
			getRequest().setAttribute("saveMsg", "管理员保存或修改失败!");
		}
		getRequest().setAttribute("editAdmin", admin);
		// ******************************
		adminListPage(adminService);
		return "adminList";
	}

	public String delete() {
		AdminService adminService = new AdminService();
		if (adminService.deleteAdmin(TranslateHelper.parseInteger(id))) {
			getRequest().setAttribute("saveMsg", "管理员删除成功!");
		} else {
			getRequest().setAttribute("saveMsg", "管理员删除失败!");
		}
		adminListPage(adminService);
		return "adminList";
	}

	public String adminRole() {
		adminRole(null);
		return "admin-role";
	}

	public String adminRoleSave() {
		AdminService adminService = new AdminService();
		if (adminService.saveAdminRole(TranslateHelper.parseInteger(id), check)) {
			getRequest().setAttribute("saveMsg", "管理员角色设置成功!");
		} else {
			getRequest().setAttribute("saveMsg", "管理员角色设置失败!");
		}
		// *********************
		adminRole(null);
		return "admin-role";
	}

	public String validate() {
		HttpSession session = getRequest().getSession();
		String param = getActionMethod();
		if ("login".equals(param)) {
			/*if (username == null || "".equals(username.trim())) {
				addError("loginMsg", "请输入登录信息!");
			} else if (password == null || "".equals(password.trim())) {
				addError("loginMsg", "请输入登录信息!");
			} else if (randcode == null || "".equals(randcode.trim())) {
				addError("loginMsg", "请输入登录信息!");
			} else {
				String sessionCode = (String) session.getAttribute(RandomCode.RANDOMCODE);
				if (!randcode.equalsIgnoreCase(sessionCode)) {
					addError("loginMsg", "输入内容有错误!");
				}
			}
			return "login";*/
		} else if ("self-info".equals(param)) {
			if (name == null || "".equals(name.trim())) {
				addError("infoMsg", "请输入昵称信息!");
			}
			return "self-info";
		} else if ("self-pwd".equals(param)) {
			if (oldPwd == null || "".equals(oldPwd.trim())) {
				addError("pwdMsg", "请输入旧密码信息!");
			} else if (newPwd == null || "".equals(newPwd.trim())) {
				addError("pwdMsg", "请输入新密码信息!");
			} else if (!newPwd.equals(newPwdz)) {
				addError("pwdMsg", "新密码信息输入不一致!");
			}
			return "self-pwd";
		}
		return null;
	}

	private void adminListPage(AdminService adminService) {
		PageModel model = new PageModel();
		int pageNum = TranslateHelper.parseInteger(page);
		model.setPageNum(pageNum);
		if (adminService == null)
			adminService = new AdminService();
		List<SysAdmin> admins = adminService.findAllAdmin(model);
		getRequest().setAttribute("adminList", admins);
		model.setPaths(getBasePath() + "/manager/admin/list.do?");
		getRequest().setAttribute(PageModel.PAGEMODEL, model);
	}

	private void adminRole(AdminService adminService) {
		if (adminService == null)
			adminService = new AdminService();
		List<SysRole> roles = adminService.findAllRole(null, 1);
		getRequest().setAttribute("roles", roles);
		SysAdmin admin = adminService.findAdminRole(TranslateHelper.parseInteger(id));
		getRequest().setAttribute("selectedAdmin", admin);
		// ***********************
		adminListPage(adminService);
	}

	private String getBasePath() {
		String basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
		return basePath;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRandcode() {
		return randcode;
	}

	public void setRandcode(String randcode) {
		this.randcode = randcode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getNewPwdz() {
		return newPwdz;
	}

	public void setNewPwdz(String newPwdz) {
		this.newPwdz = newPwdz;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUpdatePwd() {
		return updatePwd;
	}

	public void setUpdatePwd(String updatePwd) {
		this.updatePwd = updatePwd;
	}

	public String[] getCheck() {
		return check;
	}

	public void setCheck(String[] check) {
		this.check = check;
	}

}
